#include <bits/stdc++.h>
using namespace std;
#define int long long

void solve() {
    int n;
    cin >> n;
    int ans = INT_MAX;
    int a = INT_MAX, b = INT_MAX;
    for(int i = 0; i < n; i++) {
        int m;
        string s;
        cin >> m >> s;
        if(s == "00") continue;
        else if(s == "11") ans = min(ans, m);
        else if(s == "01") b = min(b, m);
        else a = min(a, m);
    }
    if(ans == INT_MAX && (a == INT_MAX || b == INT_MAX)) {
        cout << "-1" << endl;
    }else{
        cout << min(ans, a + b) << endl;
    }
}

signed main() {
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}